import { Component } from '@angular/core';
import {HttpService} from "../../providers/HttpService";
import {NavController} from "ionic-angular";

/**
 * Generated class for the HomeInfoComponent component.
 *
 * See https://angular.io/docs/ts/latest/api/core/index/ComponentMetadata-class.html
 * for more info on Angular Components.
 */
@Component({
  selector: 'home-info',
  templateUrl: 'home-info.html'
})
export class HomeInfoComponent {

    tab_position: number = 0;
    tab_list: any[] = [
        {
            title: '要闻',
            value: 1
        },
        {
            title: '贵金属',
            value: 8
        }
    ];
    news_list: string;

    constructor(private navCtrl: NavController, private httpService: HttpService) {
        console.log('Hello HomeInfoComponent Component');
    }

    ngOnInit() {
        this.getNewsByChannel(this.tab_list[this.tab_position].value);
    }

    getTabBtnStyle(index: number) {
        return {
            'tab-btn-select': this.tab_position == index,
            'tab-btn-none': this.tab_position != index
        };
    }

    getNewsByChannel(id: number) {
        this.httpService.instance()
            .setUrl('/gold/getNewsByChannel')
            .setQuery({
                channelId: id
            })
            .getRequest()
            .then(res => {
                if (res.status == 0) {
                    this.news_list = res.data.pager.result;
                } else {
                    console.log(res.msg);
                }
            });
    }

    onTabBtnSelected(index: number, value: number) {
        this.tab_position = index;
        this.getNewsByChannel(value);
    }

    onInfoDetailSelected(data: any) {
        this.navCtrl.push('InfoDetailPage', {news: data});
    }
}
